import { RouteProps, Navigate, useLocation } from "react-router-dom";
import { matchRoute } from "../utils/util";
import routes from "../router";
// import { routes } from "../router";

function AuthRouter({ children }: RouteProps) {
  const token = localStorage.getItem("token") || "";
  const { pathname } = useLocation();
  const router = matchRoute(pathname, routes);
  console.log(router, "router");

  if (router.meta?.unwantedAuth) {
    return children;
  }
  if (!token) {
    return <Navigate to="/login" replace />;
  } else {
    return children;
  }
}

export default AuthRouter;
